﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BSCMFF.Core.DataAccessLayer;

namespace BSCMFF.Web.Pages.Reward_System
{
    public partial class Rewards : BSCMFF.Core.MultiLanguagePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (EvalSelection.PublicSelectedValue != 0)
            {
                PanelRewards.Visible = true;

                BSCEntities context = new BSCEntities();
                Evaluation_Period ep = (from eps in context.Evaluation_Period
                                        where eps.ID == EvalSelection.PublicSelectedValue
                                        select eps).SingleOrDefault();

                if (ep.Competencies != true)
                {
                    RadGridCompetencies.Visible = false;
                }
                else
                {
                    RadGridCompetencies.Visible = true;
                    FillTopEmployeesCompetencyGrid(EvalSelection.PublicSelectedValue);
                }

                //if (ep.PKPIs != true)
                //{
                //    RadGridPKPIs.Visible = false;
                //}
                //else
                //{
                //    RadGridPKPIs.Visible = true;
                //    FillPKPIsBonusGrid(EvalSelection.PublicSelectedValue);
                //}                
            }
        }

        private void FillPKPIsBonusGrid(int evalID)
        {
          /*  BSCEntities context = new BSCEntities();

            var data = (from epk in context.Evaluation_PKPIs
                        join ee in context.Evaluation_Evaluator_Employee on epk.EEE_FK equals ee.ID
                        where ee.Evaluation_FK == evalID
                        group epk by new { ee.Employee_FK } into g
                        join u in context.Users on g.Key.Employee_FK equals u.ID
                        join epp in context.Evaluation_Period on evalID equals epp.ID
                        orderby u.ID
                        select new
                        {
                            fullname = u.Firstname + " " + u.Lastname,
                            pkpi = g.Sum(epk => (double)epk.Weight * epk.Score) / 10,
                            needed = epp.PKPIFromScore,
                            salary = (int)u.Salary,
                            minCoef = epp.PKPIMinCoef,
                            maxCoef = epp.PKPIMaxCoef,

                            citatel = (epp.PKPIMaxCoef - epp.PKPIMinCoef),
                            citatel2 = ((g.Sum(epk => (double)epk.Weight * epk.Score) / 10) >= epp.PKPIFromScore ? ((g.Sum(epk => (double)epk.Weight * epk.Score) / 10) - epp.PKPIFromScore) : 0),
                            menovatel = (120 - epp.PKPIFromScore),
                            fromcoef = epp.PKPIMinCoef,
                            //curr = epp.PKPIMinCoef + (((epp.PKPIMaxCoef - epp.PKPIMinCoef) * ((g.Sum(epk => (double)epk.Weight * epk.Score) / 10) >= epp.PKPIFromScore ? ((g.Sum(epk => (double)epk.Weight * epk.Score) / 10) - epp.PKPIFromScore) : 0))/((120 - epp.PKPIFromScore))),

                            currOr = ((g.Sum(epk => (double)epk.Weight * epk.Score) / 10) >= epp.PKPIFromScore) ? (epp.PKPIMinCoef + (((epp.PKPIMaxCoef - epp.PKPIMinCoef) * ((g.Sum(epk => (double)epk.Weight * epk.Score) / 10) >= epp.PKPIFromScore ? ((g.Sum(epk => (double)epk.Weight * epk.Score) / 10) - epp.PKPIFromScore) : 0))/((120 - epp.PKPIFromScore)))) : 0,

                            //currCoef = epp.PKPIMinCoef + ((epp.PKPIMaxCoef - epp.PKPIMinCoef) * ((g.Sum(epk => (double)epk.Weight * epk.Score) / 10) >= epp.PKPIFromScore ? ((g.Sum(epk => (double)epk.Weight * epk.Score) / 10) - epp.PKPIFromScore) : 0) - epp.PKPIFromScore) / (120 - epp.PKPIFromScore),
                            
                            //currCoer = epp.PKPIMinCoef + (((epp.PKPIMaxCoef - epp.PKPIMinCoef)*((g.Sum(epk => (double)epk.Weight * epk.Score) / 10) >= epp.PKPIFromScore ? (int)(u.Salary * epp.PKPIMinCoef/100) : 0))/(epp.PKPIMaxCoef-epp.PKPIMinCoef)),
                            bonus = (int)u.Salary * (int)(((g.Sum(epk => (double)epk.Weight * epk.Score) / 10) >= epp.PKPIFromScore) ? (epp.PKPIMinCoef + (((epp.PKPIMaxCoef - epp.PKPIMinCoef) * ((g.Sum(epk => (double)epk.Weight * epk.Score) / 10) >= epp.PKPIFromScore ? ((g.Sum(epk => (double)epk.Weight * epk.Score) / 10) - epp.PKPIFromScore) : 0))/((120 - epp.PKPIFromScore)))) : 0) / 100,
                            hasBonus = (g.Sum(epk => (double)epk.Weight * epk.Score) / 10) >= epp.PKPIFromScore

                        });
            */
            //RadGridPKPIs.DataSource = data;
            //RadGridPKPIs.DataBind();
        
        }

        private void FillTopEmployeesCompetencyGrid(int evalID)
        {
            BSCEntities context = new BSCEntities();
            Evaluation_Period ep;

            if (EvalSelection.PublicSelectedValue == 0)
            {
                return;
            }
            else
            {
                ep = (from temp in context.Evaluation_Period
                                        where temp.ID == evalID
                                        select temp).SingleOrDefault();
            }
            if (ep.Reward != true)
            {
                return;
            }

            var data = (from ev in context.Evaluation_User_Question_Value
                        join ee in context.Evaluation_Evaluator_Employee on ev.EEE_FK equals ee.ID
                        where ee.Evaluation_FK == evalID
                        join eq in context.Evaluation_Question on ev.Evaluation_Question_FK equals eq.ID
                        join ec in context.Evaluation_Category on eq.Evaluation_Category_FK equals ec.ID
                        group ev by new {ee.Employee_FK} into g
                        join u in context.Users on g.Key.Employee_FK equals u.ID
                        join epp in context.Evaluation_Period on evalID equals epp.ID
                        orderby g.Sum(ev => ev.Value - ev.Evaluation_Question.Evaluation_Category.Min) * 100 / g.Sum(ev => ev.Evaluation_Question.Evaluation_Category.Max - ev.Evaluation_Question.Evaluation_Category.Min) descending
                        select new
                        {
                            Fullname = u.Firstname + u.Lastname,
                            perc = g.Sum(ev => ev.Value - ev.Evaluation_Question.Evaluation_Category.Min)*100/g.Sum(ev => ev.Evaluation_Question.Evaluation_Category.Max - ev.Evaluation_Question.Evaluation_Category.Min),
                        
                            salary = u.Salary == null ? 0 : (int)u.Salary,
                            bonus = u.Salary == null ? 0 : (int)(u.Salary * epp.CompetencyCoef / 100)
                        }).ToList();

            //Evaluation_Period ep = (from temp in context.Evaluation_Period
            //                        where temp.ID == evalID
            //                        select temp).SingleOrDefault();

            int topPercent = (int)ep.CompetencyTop;
            int coef = (int)ep.CompetencyCoef;

            int totalEvaluatedEmployees = data.Count;

            int i = 0;

            foreach (var d in data)
            {
                if (i++ < 1)
                {
                    string fn = d.Fullname;
                }
            }

            RadGridCompetencies.DataSource = data;
            RadGridCompetencies.DataBind();
        }
    }
}